﻿ 
/**
* 命名空间: Game.TDBehaviorTree
*
* 功 能： 播放动画
* 类 名： NodePlayAnim
*
* Ver 变更日期 负责人 变更内容
* ───────────────────────────────────
* V0.01 2019/12/13 15:06:51  
*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEditor;
using Newtonsoft.Json;
using UnityEditor.Animations;

namespace Game.TDBehaviorTree
{
    [System.Serializable]
    public class NodePlayAnim : NodeAction
    {
        [JsonProperty("animName")]
        public string m_AnimName;


        private bool m_IsTextEditor;
        private GameObject m_Obj;
        private Animator m_Anim;
        private float m_SliderValue;
        public NodePlayAnim() : base(NodeType.Action)
        {
        }

        public override NodeResultType Execute()
        {
            UnityEngine.Debug.Log("播放动画:" + m_AnimName);
            m_Agent.GetComponent<Animator>().SetTrigger(m_AnimName);
            return NodeResultType.Success;
        }

        public override void Draw(Rect m_Rect, GUIStyle style, NodeValue _value)
        {
            base.Draw(m_Rect, style, _value);
            GUILayout.BeginHorizontal(GUILayout.Height(20));
            EditorGUILayout.LabelField("动画Id", GUILayout.Width(40));
            m_AnimName = EditorGUILayout.TextField(m_AnimName, GUILayout.Width(80), GUILayout.Height(20));
            _value.m_NodeArgs[0] = m_AnimName;
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal(GUILayout.Height(20));
            GUILayout.Label("测试是否:", GUILayout.Width(60), GUILayout.Height(20));
            m_IsTextEditor = EditorGUILayout.Toggle(m_IsTextEditor, GUILayout.Width(20), GUILayout.Height(20));
            GUILayout.EndHorizontal();


            GUILayout.BeginVertical(GUILayout.Width(100), GUILayout.Height(70));


            if (m_IsTextEditor)
            {
                m_Obj = (GameObject)EditorGUILayout.ObjectField(m_Obj, typeof(GameObject), GUILayout.Width(100), GUILayout.Height(20));
            }

            if (m_Obj != null)
            {
                if (m_Anim == null)
                {
                    m_Anim = m_Obj.GetComponent<Animator>();
                }
                if (m_Anim != null)
                {
                    GUILayout.BeginHorizontal(GUILayout.Height(110));
                    m_SliderValue = GUILayout.HorizontalSlider(m_SliderValue, 0, GetLengthByName(m_AnimName), GUILayout.Width(80), GUILayout.Height(20));
                    GUILayout.Label(m_SliderValue.ToString(), GUILayout.Width(30), GUILayout.Height(20));
                    m_Anim.Play(m_AnimName, 0, m_SliderValue);
                    GUILayout.EndHorizontal();
                }
            }


            GUILayout.EndVertical();
        }

        public override void DeRializable(NodeValue _value)
        {
            base.DeRializable(_value);
            m_AnimName = _value.m_NodeArgs[0];
        }

        public float GetLengthByName(string name)
        {
            float length = 0;
            AnimatorClipInfo[] infos = m_Anim.GetCurrentAnimatorClipInfo(0);
            AnimationClip[] clips = m_Anim.runtimeAnimatorController.animationClips;
            foreach (AnimationClip clip in clips)
            {
                if (clip.name.Equals(name))
                {
                    length = clip.length;
                    break;
                }
            }

            return length;
        }
    }
}
